
const app = getApp()
const common = require('../../../public.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        shopInfo: {},
        height: '', //胶囊位置
        top: '', //胶囊位置
        storeId: "",
     
        active: 0,
        textareaForce: false,
        commentPlachold: '我来说几句',
        articleList:[],  //总
        articleLists:[],
        pageNum:1
    },
    gong(refresh=false){
        const that = this
        const pageNum = refresh ? 1 : this.data.pageNum + 1
        let tempJson = {
            pageSize: 10,
            pageNum,
            storeId: this.data.storeId
        }
          common.post('/storeArticle/getStoreArticleListForOneStore', tempJson,
            function (res) {
            if (res.status === 200) {
               that.setData({
                articleList:res.data,
                articleLists:refresh ? res.data.list:[...res.data.list,...that.data.articleLists]
               })
            } else {
                common.showRequestErrMsg(res)
            }
            },
            function () {
            that.setData({
                isLoadingBusy: false
            })
            })  
    },
         // 攻略展开更多
         showMore(){
            console.log("看的到")
         this.gong(false)
        },
        guanMore(){
            this.gong(true)
        },
    // 点击定位
    click_address() {
        if (!this.data.shopInfo.store.xpoint || !this.data.shopInfo.store.ypoint) {
            common.showToast('商家未设置位置信息')
            return
        }
        let lon = parseFloat(this.data.shopInfo.store.xpoint)
        let lat = parseFloat(this.data.shopInfo.store.ypoint)
        console.log(this.data.shopInfo.store.xpoint)
        console.log(this.data.shopInfo.store.ypoint)
        wx.openLocation({
            latitude: lat,
            longitude: lon,
            scale: 18,
            name: this.data.shopInfo.store.name ? this.data.shopInfo.store.name : null,
            address: this.data.shopInfo.store.address ? this.data.shopInfo.store.address : null,
            fail: () => {
                wx.showToast({
                    title: '无地址信息',
                    icon: "none"
                })
            }
        })
    },
    // 拨打电话
    call() {
        wx.makePhoneCall({
            phoneNumber: this.data.shopInfo.store.mobile,
            fail: function () { }
        })
    },

    goBack() {
        const pages = getCurrentPages();
        const prevPage = pages[pages.length - 2]; //上一个页面
        // 从朋友圈进入的时候，页面栈为空，这里需要判断是否有上个页面
        if (prevPage) {
            wx.navigateBack({
                delta: 1
            })
        } else {
            wx.redirectTo({
                url: '/pages/index/index',
            })
        }
    },

    clickCollect() {
        let that = this
        wx.getStorage({ //获取本地缓存
            key: 'user',
            success(res) {
                that.setData({
                    userInfo: res.data.user,
                    openid: res.data.openid,
                }, function () {
                      // 当前接口200是关注，非200是未关注
                    let param = that.data.isCollected ? 'delete' : 'add'
                    var tempJson = {
                        token: that.data.token,
                        userId: that.data.userInfo.id,
                        storeId: that.data.storeId
                    };
                    common.post("/store/favo/" + param, tempJson, function (res) {
                        res.status == 200 && that.queryCollection()
                    })
                })
            },
            fail: function (res) {
                common.showLoginPage()
            },
        })

    },
    queryCollection() {
        var that = this
        var tempJson = {
            token: that.data.token,
            userId: that.data.userInfo.id,
            storeId: that.data.storeId
        };
        common.post("/store/favo/collected", tempJson, function (res) {
            // 当前接口200是关注，非200是未关注
            that.setData({
                isCollected: res.data
            })
        });
    },
    onLoad: function (options) {
        let capsule = wx.getMenuButtonBoundingClientRect()
        this.setData({
            height: capsule.height,
            top: capsule.top,
            storeId: options.id,

        })
        common.getLocalUserInfo((success, data) => {
            if (success) {
                this.setData({
                    userInfo: data.user,
                    token: data.token,
                    openid: data.openid,

                }, () => {
                    this.gong(true)
                    this.queryCollection()
                })
            } else {

            }
        })
        // 定位
        app.getLocation(
            (data) => {
                this.setData({
                    lon: data.longitude,
                    lat: data.latitude
                }, () => {
                    this.shopInfo()
                })
            },
            (err) => {
                // 定位失败回调
                this.setData({
                    refreshing: false
                })
            })
    },
    goodsDetail(e) {
        common.gotoGoodsPage(e.currentTarget.dataset.item)
    },
    toArticleDetail(e) {
        const item = e.currentTarget.dataset.item
        wx.navigateTo({
          url: '/packageD/pages/discovery/articleDetail/index?id=' + item.id
            + '&storeName=' + item.storeName
            + '&title=' + item.title
            + '&createTime=' + item.createTime
            + '&storeImg=' + item.storeImg
        })
      },
    // 时间戳
    time(time) {
        return common.dateFormat('yyyy-MM-dd HH:mm', time)
    },
    //   领取

    immediateClick(e) {
        const idx = e.currentTarget.dataset.id
        const ite = e.currentTarget.dataset.ite
        // console.log(ite,"5555")
        var that = this;
        wx.getStorage({
            key: 'user',
            success: function (res) {
                that.setData({
                    userInfo: res.data.user,
                    token: res.data.token,
                    openid: res.data.openid
                    // itemId: that.options.id,
                })
                that.getVoucher(idx, ite);
            },
            fail: function (res) {
                common.showLoginPage()
            }
        })
    },
    getVoucher(idx, ite) {
        var that = this;
        var tempJson = {
            discountCouponId: idx,
            source: 1, // 1为手动领取，2为系统群发
            token: that.data.token,
            userId: that.data.userInfo.id
        };
        common.wxRequest("/discountCoupon/getShopCoupon", "get", tempJson, 'application/x-www-form-urlencoded', function (res) {
            console.log(res);
            if (res.status == 200) {
                wx.showToast({
                    title: '领取成功',
                    icon: "none"
                })
            } else {
                wx.showToast({
                    title: res.msg ? res.msg : '领取失败',
                    icon: "none"
                })

                if (res.msg == "该用户已领取") {
                    for (var i = 0; i < that.data.couponList.length; i++) {
                        if (that.data.couponList[i].id == idx) {
                            that.data.couponList[i]['isGet'] = 1
                        }
                    }
                    that.setData({
                        couponList: that.data.couponList
                    })

                }


            }
        })
    },
    // 店铺详情
    async shopInfo() {
        var that = this
        var tempJson = {
            storeId: that.data.storeId,
            // storeId:33,
            xpoint: this.data.lat,
            ypoint: this.data.lon,
            xcFlag: '0' //是否含有携程门票 0不含有
        }
        common.get('/store/mainPageInfo', tempJson, res => {
            for (let i = 0; i < res.data.couponList.length; i++) {
                const startTimeStr = res.data.couponList[i].beginTime
                const endTimeStr = res.data.couponList[i].finishTime
                res.data.couponList[i].beginTime = that.timestampToTime(startTimeStr)
                res.data.couponList[i].finishTime = that.timestampToTime(endTimeStr)
            }
            if (res.data.commentList) {
                for (var i = 0; i < res.data.commentList.length; i++) {
                    res.data.commentList[i].addTime = that.time(res.data.commentList[i].addTime)
                }
            }
            this.setData({
                couponList: res.data.couponList,
                shopInfo: res.data,
                defaultItemList: res.data.itemCatList2.defaultItemList,
            })
        })


    },
    onChange(e) {
        let { value } = e.detail
        this.setData({
            commentValue: value
        })
    },
    add(e) {
        console.log(e);
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../index/index?id=' + id,
        })
    },
})